<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>popot-2.01: /opt/GoogleCode/popot/trunk/popot-2.01/src/individuals.h Source File</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />

<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>

</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">popot-2.01
   
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>File&#160;Members</span></a></li>
    </ul>
  </div>
<div class="header">
  <div class="headertitle">
<div class="title">/opt/GoogleCode/popot/trunk/popot-2.01/src/individuals.h</div>  </div>
</div>
<div class="contents">
<a href="individuals_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="preprocessor">#ifndef POPOT_INDIVIDUALS_H</span>
<a name="l00002"></a>00002 <span class="preprocessor"></span><span class="preprocessor">#define POPOT_INDIVIDUALS_H</span>
<a name="l00003"></a>00003 <span class="preprocessor"></span>
<a name="l00004"></a>00004 <span class="preprocessor">#include &lt;iostream&gt;</span>
<a name="l00005"></a>00005 <span class="preprocessor">#include &lt;cmath&gt;</span>
<a name="l00006"></a>00006 <span class="preprocessor">#include &lt;cstdlib&gt;</span>
<a name="l00007"></a>00007 <span class="preprocessor">#include &lt;cstring&gt;</span>
<a name="l00008"></a>00008 
<a name="l00009"></a>00009 <span class="preprocessor">#include &quot;<a class="code" href="maths_8h.html">maths.h</a>&quot;</span>
<a name="l00010"></a>00010 <span class="preprocessor">#include &quot;<a class="code" href="neighborhood_8h.html">neighborhood.h</a>&quot;</span>
<a name="l00011"></a>00011 <span class="preprocessor">#include &quot;<a class="code" href="exceptions_8h.html">exceptions.h</a>&quot;</span>
<a name="l00012"></a>00012 <span class="preprocessor">#include &quot;<a class="code" href="tools_8h.html">tools.h</a>&quot;</span>
<a name="l00013"></a>00013 <span class="preprocessor">#include &quot;<a class="code" href="initializers_8h.html">initializers.h</a>&quot;</span>
<a name="l00014"></a>00014 
<a name="l00015"></a>00015 <span class="comment">// For particle param values see</span>
<a name="l00016"></a>00016 <span class="comment">// For standard PSO </span>
<a name="l00017"></a>00017 <span class="comment">// Clerc, M., et al.: Standard PSO 2007</span>
<a name="l00018"></a>00018 <span class="comment">// Bratton, D., Kennedy, J.: Defining a Standard for Particle Swarm Optimization.</span>
<a name="l00019"></a>00019 
<a name="l00020"></a>00020 <span class="keyword">namespace </span>popot
<a name="l00021"></a>00021 {
<a name="l00022"></a>00022 
<a name="l00027"></a>00027   <span class="keyword">template</span>&lt; <span class="keyword">typename</span> VALUE_TYPE&gt;
<a name="l00028"></a>00028   <span class="keyword">class </span>Vector
<a name="l00029"></a>00029   {
<a name="l00030"></a>00030   <span class="keyword">protected</span>:
<a name="l00031"></a><a class="code" href="classpopot_1_1Vector.html#a9630b07a8856a54b94d940083124a720">00031</a>     <span class="keywordtype">size_t</span> <a class="code" href="classpopot_1_1Vector.html#a9630b07a8856a54b94d940083124a720">_dimension</a>;
<a name="l00032"></a><a class="code" href="classpopot_1_1Vector.html#a979e31d6c076bf2fe387393407e191dd">00032</a>     VALUE_TYPE * <a class="code" href="classpopot_1_1Vector.html#a979e31d6c076bf2fe387393407e191dd">_values</a>;
<a name="l00033"></a>00033 
<a name="l00034"></a>00034   <span class="keyword">public</span>:
<a name="l00035"></a>00035 
<a name="l00039"></a><a class="code" href="classpopot_1_1Vector.html#a10db4d32978788f19aeb930ae7c7ba9c">00039</a>     <a class="code" href="classpopot_1_1Vector.html#a10db4d32978788f19aeb930ae7c7ba9c">Vector</a>(<span class="keywordtype">void</span>) : <a class="code" href="classpopot_1_1Vector.html#a9630b07a8856a54b94d940083124a720">_dimension</a>(0), <a class="code" href="classpopot_1_1Vector.html#a979e31d6c076bf2fe387393407e191dd">_values</a>(0)
<a name="l00040"></a>00040     {}
<a name="l00041"></a>00041     
<a name="l00045"></a><a class="code" href="classpopot_1_1Vector.html#a1a331b91e1609b67e7324ffdfad9bc94">00045</a>     <a class="code" href="classpopot_1_1Vector.html#a10db4d32978788f19aeb930ae7c7ba9c">Vector</a>(<span class="keywordtype">size_t</span> dimension) 
<a name="l00046"></a>00046       : <a class="code" href="classpopot_1_1Vector.html#a9630b07a8856a54b94d940083124a720">_dimension</a>(dimension)
<a name="l00047"></a>00047     {
<a name="l00048"></a>00048       <a class="code" href="classpopot_1_1Vector.html#a979e31d6c076bf2fe387393407e191dd">_values</a> = <span class="keyword">new</span> VALUE_TYPE[<a class="code" href="classpopot_1_1Vector.html#a9630b07a8856a54b94d940083124a720">_dimension</a>];
<a name="l00049"></a>00049       <span class="keywordflow">for</span>(<span class="keywordtype">size_t</span> i = 0 ; i &lt; dimension; ++i)
<a name="l00050"></a>00050         <a class="code" href="classpopot_1_1Vector.html#a979e31d6c076bf2fe387393407e191dd">_values</a>[i] = 0;
<a name="l00051"></a>00051     }
<a name="l00052"></a>00052 
<a name="l00056"></a><a class="code" href="classpopot_1_1Vector.html#a6de5f7bc7288d6b2efc3e5b135f166e7">00056</a>     <a class="code" href="classpopot_1_1Vector.html#a10db4d32978788f19aeb930ae7c7ba9c">Vector</a>(<span class="keyword">const</span> <a class="code" href="classpopot_1_1Vector.html" title="The template parameters is the type of values (e.g. double, or bool)">Vector</a>&amp; other)
<a name="l00057"></a>00057     {
<a name="l00058"></a>00058       <a class="code" href="classpopot_1_1Vector.html#a9630b07a8856a54b94d940083124a720">_dimension</a> = other._dimension;
<a name="l00059"></a>00059       <a class="code" href="classpopot_1_1Vector.html#a979e31d6c076bf2fe387393407e191dd">_values</a> = <span class="keyword">new</span> VALUE_TYPE[<a class="code" href="classpopot_1_1Vector.html#a9630b07a8856a54b94d940083124a720">_dimension</a>];
<a name="l00060"></a>00060       <span class="keywordflow">for</span>(<span class="keywordtype">size_t</span> i = 0 ; i &lt; <a class="code" href="classpopot_1_1Vector.html#a9630b07a8856a54b94d940083124a720">_dimension</a> ; ++i)
<a name="l00061"></a>00061         <a class="code" href="classpopot_1_1Vector.html#a979e31d6c076bf2fe387393407e191dd">_values</a>[i] = other._values[i];
<a name="l00062"></a>00062     }
<a name="l00063"></a>00063 
<a name="l00067"></a><a class="code" href="classpopot_1_1Vector.html#a512e762c4f86b77533513bd7b429a27f">00067</a>     <a class="code" href="classpopot_1_1Vector.html" title="The template parameters is the type of values (e.g. double, or bool)">Vector</a> &amp; <a class="code" href="classpopot_1_1Vector.html#a512e762c4f86b77533513bd7b429a27f">operator=</a>(<span class="keyword">const</span> <a class="code" href="classpopot_1_1Vector.html" title="The template parameters is the type of values (e.g. double, or bool)">Vector</a>&amp; other)
<a name="l00068"></a>00068     {
<a name="l00069"></a>00069       <span class="keywordflow">if</span>(<a class="code" href="classpopot_1_1Vector.html#a9630b07a8856a54b94d940083124a720">_dimension</a> != other._dimension){
<a name="l00070"></a>00070         <span class="keyword">delete</span>[] <a class="code" href="classpopot_1_1Vector.html#a979e31d6c076bf2fe387393407e191dd">_values</a>;
<a name="l00071"></a>00071         <a class="code" href="classpopot_1_1Vector.html#a9630b07a8856a54b94d940083124a720">_dimension</a> = other._dimension;
<a name="l00072"></a>00072         <a class="code" href="classpopot_1_1Vector.html#a979e31d6c076bf2fe387393407e191dd">_values</a> = <span class="keyword">new</span> VALUE_TYPE[<a class="code" href="classpopot_1_1Vector.html#a9630b07a8856a54b94d940083124a720">_dimension</a>];
<a name="l00073"></a>00073       }
<a name="l00074"></a>00074       <span class="keywordflow">for</span>(<span class="keywordtype">size_t</span> i = 0 ; i &lt; <a class="code" href="classpopot_1_1Vector.html#a9630b07a8856a54b94d940083124a720">_dimension</a> ; ++i)
<a name="l00075"></a>00075         <a class="code" href="classpopot_1_1Vector.html#a979e31d6c076bf2fe387393407e191dd">_values</a>[i] = other._values[i];
<a name="l00076"></a>00076 
<a name="l00077"></a>00077       <span class="keywordflow">return</span> *<span class="keyword">this</span>;
<a name="l00078"></a>00078     }
<a name="l00079"></a>00079 
<a name="l00083"></a><a class="code" href="classpopot_1_1Vector.html#ab96675e080080246554a2579fde88032">00083</a>     <span class="keyword">virtual</span> <a class="code" href="classpopot_1_1Vector.html#ab96675e080080246554a2579fde88032">~Vector</a>(<span class="keywordtype">void</span>)
<a name="l00084"></a>00084     {
<a name="l00085"></a>00085       <span class="keyword">delete</span>[] <a class="code" href="classpopot_1_1Vector.html#a979e31d6c076bf2fe387393407e191dd">_values</a>;
<a name="l00086"></a>00086     }
<a name="l00087"></a>00087 
<a name="l00092"></a><a class="code" href="classpopot_1_1Vector.html#a906b652dbd5185b43b6c464d48a042ee">00092</a>     VALUE_TYPE&amp; <a class="code" href="classpopot_1_1Vector.html#a906b652dbd5185b43b6c464d48a042ee">operator[]</a>(<span class="keywordtype">size_t</span> index)<span class="keyword"> const </span>
<a name="l00093"></a>00093 <span class="keyword">    </span>{
<a name="l00094"></a>00094       <span class="keywordflow">if</span>(index &gt;= 0 &amp;&amp; index &lt; <a class="code" href="classpopot_1_1Vector.html#a9630b07a8856a54b94d940083124a720">_dimension</a>)
<a name="l00095"></a>00095         <span class="keywordflow">return</span> <a class="code" href="classpopot_1_1Vector.html#a979e31d6c076bf2fe387393407e191dd">_values</a>[index];
<a name="l00096"></a>00096       <span class="keywordflow">else</span>
<a name="l00097"></a>00097         <span class="keywordflow">throw</span> <a class="code" href="classpopot_1_1Exception_1_1IndexOutOfRange.html">popot::Exception::IndexOutOfRange</a>(index, <a class="code" href="classpopot_1_1Vector.html#a9630b07a8856a54b94d940083124a720">_dimension</a>);
<a name="l00098"></a>00098     }
<a name="l00099"></a>00099 
<a name="l00103"></a><a class="code" href="classpopot_1_1Vector.html#afccfb7d72c4e4255e7ef6e0a5b713e76">00103</a>     <span class="keywordtype">size_t</span> <a class="code" href="classpopot_1_1Vector.html#afccfb7d72c4e4255e7ef6e0a5b713e76">size</a>(<span class="keywordtype">void</span>)<span class="keyword"> const</span>
<a name="l00104"></a>00104 <span class="keyword">    </span>{
<a name="l00105"></a>00105       <span class="keywordflow">return</span> <a class="code" href="classpopot_1_1Vector.html#a9630b07a8856a54b94d940083124a720">_dimension</a>;
<a name="l00106"></a>00106     }
<a name="l00107"></a>00107 
<a name="l00111"></a><a class="code" href="classpopot_1_1Vector.html#aa74fc0320c51efea093baeafb62a9c21">00111</a>     VALUE_TYPE * <a class="code" href="classpopot_1_1Vector.html#aa74fc0320c51efea093baeafb62a9c21">getValuesPtr</a>(<span class="keywordtype">void</span>)
<a name="l00112"></a>00112     {
<a name="l00113"></a>00113       <span class="keywordflow">return</span> <a class="code" href="classpopot_1_1Vector.html#a979e31d6c076bf2fe387393407e191dd">_values</a>;
<a name="l00114"></a>00114     }
<a name="l00115"></a>00115 
<a name="l00119"></a><a class="code" href="classpopot_1_1Vector.html#a6d3cb96a58935fafa1020b7b5704effe">00119</a>     <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classpopot_1_1Vector.html#a6d3cb96a58935fafa1020b7b5704effe">print</a>(std::ostream &amp; os)<span class="keyword"> const</span>
<a name="l00120"></a>00120 <span class="keyword">    </span>{
<a name="l00121"></a>00121       os &lt;&lt; <span class="stringliteral">&quot;[&quot;</span>;
<a name="l00122"></a>00122       <span class="keywordflow">for</span>(<span class="keywordtype">size_t</span> i = 0 ; i &lt; <a class="code" href="classpopot_1_1Vector.html#afccfb7d72c4e4255e7ef6e0a5b713e76">size</a>() ; ++i)
<a name="l00123"></a>00123         {
<a name="l00124"></a>00124           os &lt;&lt; (*this)[i];
<a name="l00125"></a>00125           <span class="keywordflow">if</span>(i != <a class="code" href="classpopot_1_1Vector.html#afccfb7d72c4e4255e7ef6e0a5b713e76">size</a>() - 1)
<a name="l00126"></a>00126             os &lt;&lt; <span class="stringliteral">&quot;;&quot;</span>;
<a name="l00127"></a>00127         }
<a name="l00128"></a>00128       os &lt;&lt; <span class="stringliteral">&quot;]&quot;</span>;
<a name="l00129"></a>00129     }
<a name="l00130"></a>00130 
<a name="l00135"></a><a class="code" href="classpopot_1_1Vector.html#ac254b27efeb8486ee2f67821e3a21a60">00135</a>     <span class="keyword">friend</span> std::ostream &amp; <a class="code" href="classpopot_1_1Vector.html#ac254b27efeb8486ee2f67821e3a21a60">operator &lt;&lt;</a>(std::ostream &amp; os, <span class="keyword">const</span> <a class="code" href="classpopot_1_1Vector.html" title="The template parameters is the type of values (e.g. double, or bool)">Vector</a> &amp;v)
<a name="l00136"></a>00136     {
<a name="l00137"></a>00137       v.print(os);
<a name="l00138"></a>00138       <span class="keywordflow">return</span> os;
<a name="l00139"></a>00139     }
<a name="l00140"></a>00140   }; 
<a name="l00141"></a>00141 
<a name="l00142"></a>00142   <span class="keyword">namespace </span>PSO
<a name="l00143"></a>00143   {
<a name="l00144"></a><a class="code" href="namespacepopot_1_1PSO_1_1particle.html">00144</a>     <span class="keyword">namespace </span>particle
<a name="l00145"></a>00145     {
<a name="l00146"></a>00146 
<a name="l00147"></a>00147       <span class="keyword">template</span>&lt;<span class="keyword">typename</span> TVECTOR_TYPE=Vector&lt;<span class="keywordtype">double</span>&gt; &gt;
<a name="l00148"></a>00148       <span class="keyword">class </span><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html">Base</a>
<a name="l00149"></a>00149       {
<a name="l00150"></a>00150       <span class="keyword">protected</span>:
<a name="l00151"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#ae192191e8fdec71d7f4bb80a91f6c95f">00151</a>         TVECTOR_TYPE <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#ae192191e8fdec71d7f4bb80a91f6c95f">_position</a>;
<a name="l00152"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#ad229da637fc8e47d221ec8e9931c9a32">00152</a>         <span class="keywordtype">double</span> <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#ad229da637fc8e47d221ec8e9931c9a32">_fitness</a>;
<a name="l00153"></a>00153 
<a name="l00154"></a>00154       <span class="keyword">public</span>:
<a name="l00155"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#a501f7b5d9677846347077d04aea08577">00155</a>         <span class="keyword">typedef</span> TVECTOR_TYPE <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#a501f7b5d9677846347077d04aea08577">VECTOR_TYPE</a>;
<a name="l00156"></a>00156 
<a name="l00160"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#abbf79b1f345899a1847e30f1f7688dc2">00160</a>         <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#abbf79b1f345899a1847e30f1f7688dc2">Base</a>()
<a name="l00161"></a>00161           : <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#ae192191e8fdec71d7f4bb80a91f6c95f">_position</a>(),
<a name="l00162"></a>00162             <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#ad229da637fc8e47d221ec8e9931c9a32">_fitness</a>(0)
<a name="l00163"></a>00163         {}
<a name="l00164"></a>00164         
<a name="l00168"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#aa4962e05e494de0ccc3190b365a0451b">00168</a>         <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#abbf79b1f345899a1847e30f1f7688dc2">Base</a>(<span class="keywordtype">size_t</span> dimension)
<a name="l00169"></a>00169           : <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#ae192191e8fdec71d7f4bb80a91f6c95f">_position</a>(dimension),
<a name="l00170"></a>00170             <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#ad229da637fc8e47d221ec8e9931c9a32">_fitness</a>(0)
<a name="l00171"></a>00171         {}
<a name="l00172"></a>00172         
<a name="l00176"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#a293a97308904beb8bd6248e2f9f6a89a">00176</a>         <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#abbf79b1f345899a1847e30f1f7688dc2">Base</a>(<span class="keyword">const</span> <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html">Base</a> &amp; other) 
<a name="l00177"></a>00177           : <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#ae192191e8fdec71d7f4bb80a91f6c95f">_position</a>(other.<a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#ae192191e8fdec71d7f4bb80a91f6c95f">_position</a>),
<a name="l00178"></a>00178             <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#ad229da637fc8e47d221ec8e9931c9a32">_fitness</a>(other.<a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#ad229da637fc8e47d221ec8e9931c9a32">_fitness</a>)
<a name="l00179"></a>00179         {}
<a name="l00180"></a>00180 
<a name="l00184"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#a167a6a485507485d81e624adf90cd2d8">00184</a>         <span class="keyword">virtual</span> <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#a167a6a485507485d81e624adf90cd2d8">~Base</a>(<span class="keywordtype">void</span>)
<a name="l00185"></a>00185         {}
<a name="l00186"></a>00186 
<a name="l00190"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#a71d4619308ac22e30da12e3d0dd416fd">00190</a>         <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html">Base</a> &amp; <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#a71d4619308ac22e30da12e3d0dd416fd">operator=</a>(<span class="keyword">const</span> <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html">Base</a> &amp;other)
<a name="l00191"></a>00191         {
<a name="l00192"></a>00192           <span class="keywordflow">if</span> (<span class="keyword">this</span> == &amp;other) 
<a name="l00193"></a>00193             <span class="keywordflow">return</span> *<span class="keyword">this</span>;
<a name="l00194"></a>00194 
<a name="l00195"></a>00195           <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#ae192191e8fdec71d7f4bb80a91f6c95f">_position</a> = other.<a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#ae192191e8fdec71d7f4bb80a91f6c95f">_position</a>;
<a name="l00196"></a>00196           <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#ad229da637fc8e47d221ec8e9931c9a32">_fitness</a> = other.<a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#ad229da637fc8e47d221ec8e9931c9a32">_fitness</a>;
<a name="l00197"></a>00197           <span class="keywordflow">return</span> *<span class="keyword">this</span>;
<a name="l00198"></a>00198         }
<a name="l00199"></a>00199 
<a name="l00203"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#a7fe8395cc353d2c1db4815fea501d4c8">00203</a>         TVECTOR_TYPE&amp; <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#a7fe8395cc353d2c1db4815fea501d4c8">getPosition</a>()
<a name="l00204"></a>00204         {
<a name="l00205"></a>00205           <span class="keywordflow">return</span> <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#ae192191e8fdec71d7f4bb80a91f6c95f">_position</a>;
<a name="l00206"></a>00206         }
<a name="l00207"></a>00207 
<a name="l00211"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#a3f17b77cfb34be94b96ad6af314ee80f">00211</a>         <span class="keywordtype">double</span> <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#a3f17b77cfb34be94b96ad6af314ee80f">getFitness</a>(<span class="keywordtype">void</span>)<span class="keyword"> const</span>
<a name="l00212"></a>00212 <span class="keyword">        </span>{
<a name="l00213"></a>00213           <span class="keywordflow">return</span> <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#ad229da637fc8e47d221ec8e9931c9a32">_fitness</a>;
<a name="l00214"></a>00214         }
<a name="l00215"></a>00215 
<a name="l00219"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#a472a502fc0b13c78cba27bcad0add13a">00219</a>         <span class="keywordtype">void</span> <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#a472a502fc0b13c78cba27bcad0add13a">setFitness</a>(<span class="keywordtype">double</span> f)
<a name="l00220"></a>00220         {
<a name="l00221"></a>00221           <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#ad229da637fc8e47d221ec8e9931c9a32">_fitness</a> = f;
<a name="l00222"></a>00222         }
<a name="l00223"></a>00223 
<a name="l00228"></a>00228         <span class="keyword">template</span>&lt; <span class="keyword">typename</span> COST_FUNCTION&gt;
<a name="l00229"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#accec72c7e42907c822d206e1578ef4d1">00229</a>         <span class="keywordtype">double</span> <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#accec72c7e42907c822d206e1578ef4d1">evaluateFitness</a>(<span class="keyword">const</span> COST_FUNCTION&amp; cost_function)
<a name="l00230"></a>00230         {
<a name="l00231"></a>00231           <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#ad229da637fc8e47d221ec8e9931c9a32">_fitness</a> = cost_function(this-&gt;<a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#a7fe8395cc353d2c1db4815fea501d4c8">getPosition</a>());
<a name="l00232"></a>00232           <span class="keywordflow">return</span> <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#ad229da637fc8e47d221ec8e9931c9a32">_fitness</a>;
<a name="l00233"></a>00233         }
<a name="l00234"></a>00234         
<a name="l00235"></a>00235 
<a name="l00239"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#a5931d7becac3ab6ef91ae75d9535c3a9">00239</a>         <span class="keywordtype">bool</span> <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#a5931d7becac3ab6ef91ae75d9535c3a9">operator&lt;</a>(<span class="keyword">const</span> <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html">Base</a> &amp;p)<span class="keyword"> const</span>
<a name="l00240"></a>00240 <span class="keyword">        </span>{
<a name="l00241"></a>00241           <span class="keywordflow">return</span> (<a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#a7f91d4e8f039e80413c33cf3b5533c22">compare</a>(p) &lt; 0);
<a name="l00242"></a>00242         }
<a name="l00243"></a>00243 
<a name="l00250"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#a7f91d4e8f039e80413c33cf3b5533c22">00250</a>         <span class="keyword">virtual</span> <span class="keywordtype">int</span> <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#a7f91d4e8f039e80413c33cf3b5533c22">compare</a>(<span class="keyword">const</span> <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html">Base</a>&amp; p)<span class="keyword"> const</span>
<a name="l00251"></a>00251 <span class="keyword">        </span>{
<a name="l00252"></a>00252           <span class="keywordtype">double</span> myfitness = <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#a3f17b77cfb34be94b96ad6af314ee80f">getFitness</a>();
<a name="l00253"></a>00253           <span class="keywordtype">double</span> otherfitness = p.<a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#a3f17b77cfb34be94b96ad6af314ee80f">getFitness</a>();
<a name="l00254"></a>00254           <span class="keywordflow">if</span>(myfitness &lt; otherfitness)
<a name="l00255"></a>00255             <span class="keywordflow">return</span> -1;
<a name="l00256"></a>00256           <span class="keywordflow">else</span> <span class="keywordflow">if</span>(myfitness &gt; otherfitness)
<a name="l00257"></a>00257             <span class="keywordflow">return</span> 1;
<a name="l00258"></a>00258           <span class="keywordflow">else</span>
<a name="l00259"></a>00259             <span class="keywordflow">return</span> 0;
<a name="l00260"></a>00260         }
<a name="l00261"></a>00261 
<a name="l00265"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#a32c12b2b8a31591d80af37390c64a9f5">00265</a>         <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#a32c12b2b8a31591d80af37390c64a9f5">print</a>(std::ostream &amp; os)<span class="keyword"> const</span>
<a name="l00266"></a>00266 <span class="keyword">        </span>{
<a name="l00267"></a>00267           os &lt;&lt; <span class="stringliteral">&quot;Position : &quot;</span> &lt;&lt; this-&gt;<a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#ae192191e8fdec71d7f4bb80a91f6c95f">_position</a>;
<a name="l00268"></a>00268           os &lt;&lt; <span class="stringliteral">&quot; ; Fitness : &quot;</span> &lt;&lt; this-&gt;<a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#a3f17b77cfb34be94b96ad6af314ee80f">getFitness</a>();
<a name="l00269"></a>00269         }
<a name="l00270"></a>00270 
<a name="l00274"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#ad921acd620c2fa239b4786ae3fc47ce8">00274</a>         <span class="keyword">friend</span> std::ostream &amp; <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#ad921acd620c2fa239b4786ae3fc47ce8">operator &lt;&lt;</a>(std::ostream &amp; os, <span class="keyword">const</span> <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html">Base</a> &amp;v)
<a name="l00275"></a>00275         {
<a name="l00276"></a>00276           v.<a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#a32c12b2b8a31591d80af37390c64a9f5">print</a>(os);
<a name="l00277"></a>00277           <span class="keywordflow">return</span> os;
<a name="l00278"></a>00278         }
<a name="l00279"></a>00279       };
<a name="l00280"></a>00280 
<a name="l00281"></a>00281 
<a name="l00282"></a>00282       <span class="comment">// Particle introduces the notion</span>
<a name="l00283"></a>00283       <span class="comment">// of neighborhood, velocity and best position</span>
<a name="l00284"></a>00284       <span class="keyword">template</span>&lt;<span class="keyword">typename</span> TVECTOR_TYPE=Vector&lt;<span class="keywordtype">double</span>&gt; &gt;
<a name="l00285"></a>00285       <span class="keyword">class </span>Particle : <span class="keyword">public</span> Base&lt;TVECTOR_TYPE&gt;
<a name="l00286"></a>00286       {
<a name="l00287"></a>00287       <span class="keyword">private</span>:
<a name="l00288"></a>00288         <span class="keyword">typedef</span> Base&lt;TVECTOR_TYPE&gt; TSuper;
<a name="l00289"></a>00289         <span class="keyword">typedef</span> Particle&lt;TVECTOR_TYPE&gt; ThisParticleType;
<a name="l00290"></a>00290 
<a name="l00291"></a>00291       <span class="keyword">public</span>:
<a name="l00292"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#ae714c7c4871ef295ea0d7451b04df0cf">00292</a>         <span class="keyword">typedef</span> <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html">TSuper</a> <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#ae714c7c4871ef295ea0d7451b04df0cf">BestType</a>;
<a name="l00293"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#aa44ea6ac3cd2e1396f73aaf30c93b513">00293</a>         <span class="keyword">typedef</span> popot<a class="code" href="classpopot_1_1PSO_1_1neighborhood_1_1Neighborhood.html">::PSO::neighborhood::Neighborhood&lt; ThisParticleType &gt;</a> <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#aa44ea6ac3cd2e1396f73aaf30c93b513">NeighborhoodType</a>;
<a name="l00294"></a>00294 
<a name="l00295"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#ab66eb4e43a68d5f454ea4891a40e861a">00295</a>         TVECTOR_TYPE <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#ab66eb4e43a68d5f454ea4891a40e861a">_velocity</a>;
<a name="l00296"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#a7f1a0546236dac2736a185efb0d05ea4">00296</a>         <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html">BestType</a> <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#a7f1a0546236dac2736a185efb0d05ea4">_best_position</a>;
<a name="l00297"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#a95561ca312e67675f04ccd5fc842037c">00297</a>         <a class="code" href="classpopot_1_1PSO_1_1neighborhood_1_1Neighborhood.html">NeighborhoodType</a> <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#a95561ca312e67675f04ccd5fc842037c">_neighborhood</a>;
<a name="l00298"></a>00298           
<a name="l00299"></a>00299       <span class="keyword">public</span>:
<a name="l00300"></a>00300 
<a name="l00304"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#afd33b22591f31959507ce14eb0130dbd">00304</a>         <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#afd33b22591f31959507ce14eb0130dbd">Particle</a>() 
<a name="l00305"></a>00305           : <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html">TSuper</a>(),
<a name="l00306"></a>00306             <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#a7f1a0546236dac2736a185efb0d05ea4">_best_position</a>(),
<a name="l00307"></a>00307             <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#a95561ca312e67675f04ccd5fc842037c">_neighborhood</a>()
<a name="l00308"></a>00308         {}
<a name="l00309"></a>00309 
<a name="l00313"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#a40ec1b6f5de3ebfdbd5df840101e6c5a">00313</a>         <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#afd33b22591f31959507ce14eb0130dbd">Particle</a>(<span class="keywordtype">size_t</span> dimension) 
<a name="l00314"></a>00314           : <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html">TSuper</a>(dimension), 
<a name="l00315"></a>00315             <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#ab66eb4e43a68d5f454ea4891a40e861a">_velocity</a>(dimension),
<a name="l00316"></a>00316             <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#a7f1a0546236dac2736a185efb0d05ea4">_best_position</a>(dimension),
<a name="l00317"></a>00317             <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#a95561ca312e67675f04ccd5fc842037c">_neighborhood</a>()
<a name="l00318"></a>00318         {}
<a name="l00319"></a>00319 
<a name="l00323"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#a63a8672d03c2cc3cd79faf36835c94fc">00323</a>         <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#afd33b22591f31959507ce14eb0130dbd">Particle</a>(<span class="keyword">const</span> <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html">Particle</a>&amp; other) 
<a name="l00324"></a>00324           : <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html">TSuper</a>(other), 
<a name="l00325"></a>00325             <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#ab66eb4e43a68d5f454ea4891a40e861a">_velocity</a>(other.<a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#ab66eb4e43a68d5f454ea4891a40e861a">_velocity</a>),
<a name="l00326"></a>00326             <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#a7f1a0546236dac2736a185efb0d05ea4">_best_position</a>(other.<a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#a7f1a0546236dac2736a185efb0d05ea4">_best_position</a>),
<a name="l00327"></a>00327             <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#a95561ca312e67675f04ccd5fc842037c">_neighborhood</a>(other.<a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#a95561ca312e67675f04ccd5fc842037c">_neighborhood</a>)
<a name="l00328"></a>00328         {}
<a name="l00329"></a>00329 
<a name="l00333"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#a5a321e90d6b72ae4a50f8d658a5f9eb8">00333</a>         <span class="keyword">virtual</span> <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#a5a321e90d6b72ae4a50f8d658a5f9eb8">~Particle</a>(<span class="keywordtype">void</span>)
<a name="l00334"></a>00334         {}
<a name="l00335"></a>00335 
<a name="l00339"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#aa54ae810436504fee17569cc48260e62">00339</a>         TVECTOR_TYPE&amp; <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#aa54ae810436504fee17569cc48260e62">getVelocity</a>(<span class="keywordtype">void</span>)
<a name="l00340"></a>00340         {
<a name="l00341"></a>00341           <span class="keywordflow">return</span> <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#ab66eb4e43a68d5f454ea4891a40e861a">_velocity</a>;
<a name="l00342"></a>00342         }
<a name="l00343"></a>00343 
<a name="l00347"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#ac2642be85ba4fc064efecad2825f5410">00347</a>         <span class="keywordtype">void</span> <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#ac2642be85ba4fc064efecad2825f5410">initBestPosition</a>()
<a name="l00348"></a>00348         {
<a name="l00349"></a>00349           <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#a7f1a0546236dac2736a185efb0d05ea4">_best_position</a> = *<span class="keyword">this</span>;
<a name="l00350"></a>00350         }
<a name="l00351"></a>00351 
<a name="l00355"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#a8d1cf426edc9d3261d6a6a4e9c2a110c">00355</a>         <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html">BestType</a>&amp; <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#a8d1cf426edc9d3261d6a6a4e9c2a110c">getBestPosition</a>(<span class="keywordtype">void</span>)
<a name="l00356"></a>00356         {
<a name="l00357"></a>00357           <span class="keywordflow">return</span> this-&gt;<a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#a7f1a0546236dac2736a185efb0d05ea4">_best_position</a>;
<a name="l00358"></a>00358         }
<a name="l00359"></a>00359 
<a name="l00363"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#a1cfe00155f4255d52df54c0864c42e61">00363</a>         <a class="code" href="classpopot_1_1PSO_1_1neighborhood_1_1Neighborhood.html">NeighborhoodType</a>&amp; <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#a1cfe00155f4255d52df54c0864c42e61">getNeighborhood</a>(<span class="keywordtype">void</span>)
<a name="l00364"></a>00364         {
<a name="l00365"></a>00365           <span class="keywordflow">return</span> <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#a95561ca312e67675f04ccd5fc842037c">_neighborhood</a>;
<a name="l00366"></a>00366         }
<a name="l00367"></a>00367           
<a name="l00372"></a><a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#a6f02b4e144a100a4e3889a7c6aa1fdfb">00372</a>         <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#a6f02b4e144a100a4e3889a7c6aa1fdfb">print</a>(std::ostream &amp; os)<span class="keyword"> const</span>
<a name="l00373"></a>00373 <span class="keyword">        </span>{
<a name="l00374"></a>00374           <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Base.html#a32c12b2b8a31591d80af37390c64a9f5">TSuper::print</a>(os);
<a name="l00375"></a>00375           os &lt;&lt; <span class="stringliteral">&quot;; Velocity : &quot;</span> &lt;&lt; <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#ab66eb4e43a68d5f454ea4891a40e861a">_velocity</a>;
<a name="l00376"></a>00376           os &lt;&lt; <span class="stringliteral">&quot;; Best position : &quot;</span> &lt;&lt; <a class="code" href="classpopot_1_1PSO_1_1particle_1_1Particle.html#a7f1a0546236dac2736a185efb0d05ea4">_best_position</a>;
<a name="l00377"></a>00377         }
<a name="l00378"></a>00378 
<a name="l00379"></a>00379       };
<a name="l00380"></a>00380 
<a name="l00381"></a>00381       <span class="comment">// Generic utilitary functions used to templatify the particles</span>
<a name="l00382"></a>00382 
<a name="l00383"></a>00383       <span class="keyword">template</span>&lt;<span class="keyword">typename</span> PARTICLE&gt;
<a name="l00384"></a><a class="code" href="namespacepopot_1_1PSO_1_1particle.html#a708ed7f18a65a1759caa4d6ec00eb4df">00384</a>       <span class="keywordtype">void</span> <a class="code" href="namespacepopot_1_1PSO_1_1particle.html#a708ed7f18a65a1759caa4d6ec00eb4df">updateBestPosition</a>(PARTICLE&amp; p)
<a name="l00385"></a>00385       {
<a name="l00386"></a>00386         <span class="comment">// Update the best position the particle ever had</span>
<a name="l00387"></a>00387         <span class="comment">// with a copy of the current position</span>
<a name="l00388"></a>00388         <span class="keywordflow">if</span>(p.compare(p.getBestPosition()) &lt; 0)
<a name="l00389"></a>00389           p._best_position = p;
<a name="l00390"></a>00390       }
<a name="l00391"></a>00391 
<a name="l00392"></a>00392 
<a name="l00396"></a>00396       <span class="keyword">template</span>&lt;<span class="keyword">typename</span> PARTICLE&gt;
<a name="l00397"></a><a class="code" href="namespacepopot_1_1PSO_1_1particle.html#a32c55900dc8d5fc3e89edf922d2f147d">00397</a>       <span class="keywordtype">void</span> <a class="code" href="namespacepopot_1_1PSO_1_1particle.html#a32c55900dc8d5fc3e89edf922d2f147d">updatePosition</a>(PARTICLE&amp; p)
<a name="l00398"></a>00398       {
<a name="l00399"></a>00399         <span class="comment">// Here it is simply : p_{k+1} = p_k + v_k      </span>
<a name="l00400"></a>00400         <span class="keywordflow">for</span>(<span class="keywordtype">size_t</span> i = 0 ; i &lt; p.getPosition().size() ; ++i)
<a name="l00401"></a>00401           p.getPosition()[i] = p.getPosition()[i] + p.getVelocity()[i];
<a name="l00402"></a>00402         
<a name="l00403"></a>00403       }
<a name="l00404"></a>00404 
<a name="l00408"></a>00408       <span class="keyword">template</span>&lt;<span class="keyword">typename</span> PARTICLE, <span class="keyword">typename</span> PARAMS&gt;
<a name="l00409"></a><a class="code" href="namespacepopot_1_1PSO_1_1particle.html#a191462f5ac6280a3c1bb6e819fa597ea">00409</a>       <span class="keywordtype">void</span> <a class="code" href="namespacepopot_1_1PSO_1_1particle.html#a191462f5ac6280a3c1bb6e819fa597ea">updateVelocity_spso2006</a>(PARTICLE&amp; p)
<a name="l00410"></a>00410       {
<a name="l00411"></a>00411         <span class="comment">// The update of the velocity is done according to the equation :</span>
<a name="l00412"></a>00412         <span class="comment">// v = w * v + c r1 (best_p - p) + c r2 (best_g - p)</span>
<a name="l00413"></a>00413         <span class="comment">// with :</span>
<a name="l00414"></a>00414         <span class="comment">// r_p, r_g two random real numbers in [0.0,1.0]</span>
<a name="l00415"></a>00415         <span class="comment">// w, c1, c2 : user defined parameters</span>
<a name="l00416"></a>00416         <span class="comment">// best_p : the best position the particle ever had</span>
<a name="l00417"></a>00417         <span class="comment">// best_g : the best position the neighborhood ever had</span>
<a name="l00418"></a>00418         
<a name="l00419"></a>00419         <span class="keywordtype">double</span> r1,r2;
<a name="l00420"></a>00420         <span class="keyword">typename</span> PARTICLE::VECTOR_TYPE&amp; pos = p.getPosition();
<a name="l00421"></a>00421         <span class="keyword">typename</span> PARTICLE::VECTOR_TYPE&amp; vel = p.getVelocity();
<a name="l00422"></a>00422 
<a name="l00423"></a>00423         <span class="keywordflow">for</span>(<span class="keywordtype">size_t</span> i = 0 ; i &lt; pos.size(); ++i)
<a name="l00424"></a>00424           {
<a name="l00425"></a>00425             r1 = <a class="code" href="namespacepopot_1_1initializer_1_1position.html#a76bfea94ffddf8b069755537a05f1bab">popot::math::uniform_random</a>(0.0, PARAMS::c());
<a name="l00426"></a>00426             r2 = <a class="code" href="namespacepopot_1_1initializer_1_1position.html#a76bfea94ffddf8b069755537a05f1bab">popot::math::uniform_random</a>(0.0, PARAMS::c());
<a name="l00427"></a>00427             vel[i] = PARAMS::w() * vel[i]
<a name="l00428"></a>00428               + r1 * (p.getBestPosition().getPosition()[i] - pos[i])
<a name="l00429"></a>00429               + r2 * (p.getNeighborhood().getBest()-&gt;getPosition()[i] - pos[i]);
<a name="l00430"></a>00430           }
<a name="l00431"></a>00431       }
<a name="l00432"></a>00432 
<a name="l00433"></a>00433 
<a name="l00437"></a>00437       <span class="keyword">template</span>&lt;<span class="keyword">typename</span> PARTICLE, <span class="keyword">typename</span> PARAMS&gt;
<a name="l00438"></a><a class="code" href="namespacepopot_1_1PSO_1_1particle.html#a1ac0e8f839b165660f573ba7e4a7dc4c">00438</a>       <span class="keywordtype">void</span> <a class="code" href="namespacepopot_1_1PSO_1_1particle.html#a1ac0e8f839b165660f573ba7e4a7dc4c">updateVelocity_spso2007</a>(PARTICLE &amp;p)
<a name="l00439"></a>00439       {
<a name="l00440"></a>00440         <span class="comment">// The update of the velocity is done according to the equation :</span>
<a name="l00441"></a>00441         <span class="comment">// v = w * v + r1 (best_p - p) + r2 (best_g - p)</span>
<a name="l00442"></a>00442         <span class="comment">// with :</span>
<a name="l00443"></a>00443         <span class="comment">// r_1, r_2 two random real numbers in [0.0,c]</span>
<a name="l00444"></a>00444         <span class="comment">// w, c : user defined parameters</span>
<a name="l00445"></a>00445         <span class="comment">// best_p : the best position the particle ever had</span>
<a name="l00446"></a>00446         <span class="comment">// best_g : the best position the neighborhood ever had</span>
<a name="l00447"></a>00447 
<a name="l00448"></a>00448         <span class="comment">// we better compare the adress of the best positions rather than their contents</span>
<a name="l00449"></a>00449         <span class="comment">// comparing vectors of double can be problematic</span>
<a name="l00450"></a>00450         <span class="keywordtype">bool</span> li_equals_pi = (&amp;(p.getBestPosition()) == p.getNeighborhood().getBest());
<a name="l00451"></a>00451         <span class="keywordtype">double</span> r1,r2;
<a name="l00452"></a>00452 
<a name="l00453"></a>00453         <span class="keyword">typename</span> PARTICLE::VECTOR_TYPE&amp; pos = p.getPosition();
<a name="l00454"></a>00454         <span class="keyword">typename</span> PARTICLE::VECTOR_TYPE&amp; vel = p.getVelocity();
<a name="l00455"></a>00455 
<a name="l00456"></a>00456         <span class="keywordflow">if</span>(li_equals_pi)
<a name="l00457"></a>00457           {
<a name="l00458"></a>00458             <span class="keywordflow">for</span>(<span class="keywordtype">size_t</span> i = 0 ; i &lt; vel.size() ; ++i)
<a name="l00459"></a>00459               {
<a name="l00460"></a>00460                 r1 = <a class="code" href="namespacepopot_1_1initializer_1_1position.html#a76bfea94ffddf8b069755537a05f1bab">popot::math::uniform_random</a>(0.0, PARAMS::c());
<a name="l00461"></a>00461                 vel[i] = PARAMS::w() * vel[i] + r1 * (p.getBestPosition().getPosition()[i] - pos[i]);
<a name="l00462"></a>00462               }
<a name="l00463"></a>00463           }
<a name="l00464"></a>00464         <span class="keywordflow">else</span>
<a name="l00465"></a>00465           {
<a name="l00466"></a>00466             <span class="keywordflow">for</span>(<span class="keywordtype">size_t</span> i = 0 ; i &lt; vel.size() ; ++i)
<a name="l00467"></a>00467               {
<a name="l00468"></a>00468                 r1 = <a class="code" href="namespacepopot_1_1initializer_1_1position.html#a76bfea94ffddf8b069755537a05f1bab">popot::math::uniform_random</a>(0.0, PARAMS::c());
<a name="l00469"></a>00469                 r2 = <a class="code" href="namespacepopot_1_1initializer_1_1position.html#a76bfea94ffddf8b069755537a05f1bab">popot::math::uniform_random</a>(0.0, PARAMS::c());
<a name="l00470"></a>00470                 vel[i] = PARAMS::w() * vel[i]
<a name="l00471"></a>00471                   + r1 * (p.getBestPosition().getPosition()[i] - pos[i])
<a name="l00472"></a>00472                   + r2 * (p.getNeighborhood().getBest()-&gt;getPosition()[i] - pos[i]);
<a name="l00473"></a>00473               }
<a name="l00474"></a>00474           }
<a name="l00475"></a>00475       }
<a name="l00476"></a>00476 
<a name="l00477"></a>00477 
<a name="l00481"></a>00481       <span class="keyword">template</span>&lt;<span class="keyword">typename</span> PARTICLE, <span class="keyword">typename</span> PARAMS&gt;
<a name="l00482"></a><a class="code" href="namespacepopot_1_1PSO_1_1particle.html#abc673fa39f56d68e6994a68328f2398b">00482</a>       <span class="keywordtype">void</span> <a class="code" href="namespacepopot_1_1PSO_1_1particle.html#abc673fa39f56d68e6994a68328f2398b">updateVelocity_spso2011</a>(PARTICLE &amp;p)
<a name="l00483"></a>00483       {
<a name="l00484"></a>00484 
<a name="l00485"></a>00485           <span class="comment">// The update of the velocity is done according to the equation :</span>
<a name="l00486"></a>00486           <span class="comment">// v_i(t+1) = w v_i(t) + x&#39;_i(t) - x_i(t)</span>
<a name="l00487"></a>00487           <span class="comment">// where x&#39;_i is normally sampled from the hypersphere (Gi, Ri)</span>
<a name="l00488"></a>00488           <span class="comment">// with Gi the center and Ri the radius and</span>
<a name="l00489"></a>00489           <span class="comment">// Gi = 1/3 ( x_i + (x_i + c(p_i - x_i)) + (x_i + c(l_i - x_i)))</span>
<a name="l00490"></a>00490           <span class="comment">//    = 1/3 ( x_i +          p1          +            p2     )))</span>
<a name="l00491"></a>00491           <span class="comment">// or</span>
<a name="l00492"></a>00492           <span class="comment">// Gi = 1/2 (x_i + (x_i + c(p_i - x_i))) if p_i == l_i</span>
<a name="l00493"></a>00493           <span class="comment">//    = 1/2 (x_i +          p1         )</span>
<a name="l00494"></a>00494           <span class="comment">// Ri = ||G_i - x_i||</span>
<a name="l00495"></a>00495 
<a name="l00496"></a>00496         <span class="keywordtype">double</span> * xpi = <span class="keyword">new</span> <span class="keywordtype">double</span>[p.getPosition().size()];
<a name="l00497"></a>00497         <span class="keywordtype">double</span> * p1 = <span class="keyword">new</span> <span class="keywordtype">double</span>[p.getPosition().size()];
<a name="l00498"></a>00498         <span class="keywordtype">double</span> * p2 = <span class="keyword">new</span> <span class="keywordtype">double</span>[p.getPosition().size()];
<a name="l00499"></a>00499         <span class="keywordtype">double</span> * gr = <span class="keyword">new</span> <span class="keywordtype">double</span>[p.getPosition().size()];
<a name="l00500"></a>00500 
<a name="l00501"></a>00501 
<a name="l00502"></a>00502           <span class="keywordtype">size_t</span> i;
<a name="l00503"></a>00503 
<a name="l00504"></a>00504           <span class="comment">// We first check if the local best and personal best are identical by computing the norm of the difference</span>
<a name="l00505"></a>00505           <span class="comment">// between both positions</span>
<a name="l00506"></a>00506           <span class="comment">// This can be done with pointer comparison</span>
<a name="l00507"></a>00507           <span class="comment">// as the neighborhood holds a pointer to the best personal best</span>
<a name="l00508"></a>00508           <span class="keywordtype">bool</span> li_equals_pi = (&amp;(p.getBestPosition()) == p.getNeighborhood().getBest());
<a name="l00509"></a>00509 
<a name="l00510"></a>00510           <span class="keyword">typename</span> PARTICLE::VECTOR_TYPE&amp; pos = p.getPosition();
<a name="l00511"></a>00511           <span class="keyword">typename</span> PARTICLE::VECTOR_TYPE&amp; vel = p.getVelocity();
<a name="l00512"></a>00512 
<a name="l00513"></a>00513           <span class="comment">// First position</span>
<a name="l00514"></a>00514           <span class="comment">// p1 = xi + c * (pi - xi)</span>
<a name="l00515"></a>00515           <span class="comment">// where pi is the personal best position</span>
<a name="l00516"></a>00516           <span class="keywordflow">for</span>(i = 0 ; i &lt; pos.size() ; ++i)
<a name="l00517"></a>00517             p1[i] = pos[i] + PARAMS::c() *(p.getBestPosition().getPosition()[i] - pos[i]);
<a name="l00518"></a>00518           
<a name="l00519"></a>00519           <span class="comment">// Second position</span>
<a name="l00520"></a>00520           <span class="comment">// p2 = xi + c * (li - xi)</span>
<a name="l00521"></a>00521           <span class="comment">// where li is the local best position (within the neighborhood)</span>
<a name="l00522"></a>00522           <span class="keywordflow">for</span>(i = 0 ; i &lt; pos.size() ; ++i)
<a name="l00523"></a>00523             p2[i] = pos[i] + PARAMS::c() *(p.getNeighborhood().getBest()-&gt;getPosition()[i] - pos[i]);
<a name="l00524"></a>00524           
<a name="l00525"></a>00525           <span class="comment">// Compute the gravity center of p1, p2 and xi</span>
<a name="l00526"></a>00526           <span class="keywordflow">if</span>(!li_equals_pi)
<a name="l00527"></a>00527             {
<a name="l00528"></a>00528               <span class="comment">// We here consider p1, p2 and xi</span>
<a name="l00529"></a>00529               <span class="keywordflow">for</span>(i = 0 ; i &lt; pos.size() ; ++i)
<a name="l00530"></a>00530                 gr[i] = 1.0/3.0 * (pos[i] + p1[i] + p2[i]);
<a name="l00531"></a>00531             }
<a name="l00532"></a>00532           <span class="keywordflow">else</span>
<a name="l00533"></a>00533             {
<a name="l00534"></a>00534               <span class="comment">// We here consider only p1 (or p2, since they are equal) and xi</span>
<a name="l00535"></a>00535               <span class="keywordflow">for</span>(i = 0 ; i &lt; pos.size() ; ++i)
<a name="l00536"></a>00536                 gr[i] = 1.0/2.0 * (pos[i] + p1[i]);
<a name="l00537"></a>00537             }
<a name="l00538"></a>00538 
<a name="l00539"></a>00539           <span class="comment">// And the radius of the hypersphere</span>
<a name="l00540"></a>00540           <span class="keywordtype">double</span> ri = 0.0;
<a name="l00541"></a>00541           <span class="keywordflow">for</span>(i = 0 ; i &lt; pos.size() ; ++i)
<a name="l00542"></a>00542             ri += (gr[i] - pos[i])*(gr[i] - pos[i]);
<a name="l00543"></a>00543           ri = sqrt(ri);
<a name="l00544"></a>00544 
<a name="l00545"></a>00545           <span class="comment">// Compute the auxiliary position x&#39;_i randomly within the hypersphere (gr, ri);</span>
<a name="l00546"></a>00546           <span class="comment">// To uniformely sample from the hypersphere we uniformely sample a direction</span>
<a name="l00547"></a>00547           <span class="keywordtype">double</span> norm = 0.0;
<a name="l00548"></a>00548           <span class="keywordflow">for</span>(i = 0 ; i &lt; pos.size() ; ++i)
<a name="l00549"></a>00549             {
<a name="l00550"></a>00550               xpi[i] = <a class="code" href="namespacepopot_1_1math.html#a379cb433fc4a216e75252d7278c161a7">popot::math::normal</a>(0.0,1.0);
<a name="l00551"></a>00551               norm += xpi[i] * xpi[i];
<a name="l00552"></a>00552             }
<a name="l00553"></a>00553           norm = sqrt(norm);
<a name="l00554"></a>00554           
<a name="l00555"></a>00555           <span class="comment">// And then scale by a random radius</span>
<a name="l00556"></a>00556           <span class="keywordtype">double</span> r = <a class="code" href="namespacepopot_1_1initializer_1_1position.html#a76bfea94ffddf8b069755537a05f1bab">popot::math::uniform_random</a>(0.0,1.0);
<a name="l00557"></a>00557           <span class="keywordflow">for</span>(i = 0 ; i &lt; pos.size() ; ++i)
<a name="l00558"></a>00558             xpi[i] =  gr[i] + r * ri * xpi[i] / norm;
<a name="l00559"></a>00559 
<a name="l00560"></a>00560           <span class="comment">// And then update the velocity</span>
<a name="l00561"></a>00561           <span class="keywordflow">for</span>(i = 0 ; i &lt; vel.size() ; ++i)
<a name="l00562"></a>00562             vel[i] = PARAMS::w() * vel[i] + xpi[i] - pos[i];
<a name="l00563"></a>00563 
<a name="l00564"></a>00564       }
<a name="l00565"></a>00565       
<a name="l00566"></a>00566       <span class="comment">// 2011 !!!!!</span>
<a name="l00567"></a>00567     <span class="comment">//  /**</span>
<a name="l00568"></a>00568     <span class="comment">//   * Bounds the velocity and position of the particle</span>
<a name="l00569"></a>00569     <span class="comment">//   */</span>
<a name="l00570"></a>00570       <span class="comment">// template&lt;typename PARTICLE, typename LBOUND_FUNC, typename UBOUND_FUNC&gt;</span>
<a name="l00571"></a>00571     <span class="comment">//  virtual void confine(PARTICLE&amp; p, LBOUND_FUNC&amp; lbound, UBOUND_FUNC&amp; ubound)</span>
<a name="l00572"></a>00572     <span class="comment">//  {</span>
<a name="l00573"></a>00573     <span class="comment">//    // In case the position is out of the bounds</span>
<a name="l00574"></a>00574     <span class="comment">//    // we reset the velocities</span>
<a name="l00575"></a>00575     <span class="comment">//    for(size_t i = 0 ; i &lt; this-&gt;_dimension ; ++i)</span>
<a name="l00576"></a>00576     <span class="comment">//      {</span>
<a name="l00577"></a>00577     <span class="comment">//        if((this-&gt;getPosition(i) &lt; this-&gt;_lbound(i)))</span>
<a name="l00578"></a>00578     <span class="comment">//          {</span>
<a name="l00579"></a>00579     <span class="comment">//            this-&gt;setPosition(i, this-&gt;_lbound(i));</span>
<a name="l00580"></a>00580     <span class="comment">//            this-&gt;setVelocity(i,-0.5*this-&gt;getVelocity(i));</span>
<a name="l00581"></a>00581     <span class="comment">//          }</span>
<a name="l00582"></a>00582     <span class="comment">//        else if(this-&gt;getPosition(i) &gt; this-&gt;_ubound(i))</span>
<a name="l00583"></a>00583     <span class="comment">//          {</span>
<a name="l00584"></a>00584     <span class="comment">//            this-&gt;setPosition(i, this-&gt;_ubound(i));</span>
<a name="l00585"></a>00585     <span class="comment">//            this-&gt;setVelocity(i,-0.5*this-&gt;getVelocity(i));</span>
<a name="l00586"></a>00586     <span class="comment">//          }</span>
<a name="l00587"></a>00587     <span class="comment">//      }</span>
<a name="l00588"></a>00588     <span class="comment">//  }</span>
<a name="l00589"></a>00589     <span class="comment">//       };</span>
<a name="l00590"></a>00590       
<a name="l00591"></a>00591 
<a name="l00595"></a>00595       <span class="keyword">template</span>&lt;<span class="keyword">typename</span> PARTICLE&gt;
<a name="l00596"></a><a class="code" href="namespacepopot_1_1PSO_1_1particle.html#aa6b6d0935304d7889d4789cdbacd8599">00596</a>       <span class="keywordtype">void</span> <a class="code" href="namespacepopot_1_1PSO_1_1particle.html#aa6b6d0935304d7889d4789cdbacd8599">updatePosition_barebone</a>(PARTICLE &amp;p)
<a name="l00597"></a>00597       {
<a name="l00598"></a>00598         <span class="keyword">typename</span> PARTICLE::VECTOR_TYPE&amp; pos = p.getPosition();
<a name="l00599"></a>00599         <span class="keyword">typename</span> PARTICLE::VECTOR_TYPE&amp; vel = p.getVelocity();
<a name="l00600"></a>00600 
<a name="l00601"></a>00601         <span class="comment">// Here it is simply : p_{k+1} = v_{k+1} </span>
<a name="l00602"></a>00602         <span class="keywordflow">for</span>(<span class="keywordtype">size_t</span> i = 0 ; i &lt; pos.size() ; ++i) 
<a name="l00603"></a>00603           pos[i] = vel[i];
<a name="l00604"></a>00604       }
<a name="l00605"></a>00605 
<a name="l00606"></a>00606       <span class="keyword">template</span>&lt;<span class="keyword">typename</span> PARTICLE&gt;
<a name="l00607"></a><a class="code" href="namespacepopot_1_1PSO_1_1particle.html#a08edde97391b5f1a11043f3d30750f4d">00607</a>       <span class="keywordtype">void</span> <a class="code" href="namespacepopot_1_1PSO_1_1particle.html#a08edde97391b5f1a11043f3d30750f4d">updateVelocity_barebone</a>(PARTICLE&amp; p)
<a name="l00608"></a>00608       {
<a name="l00609"></a>00609         <span class="comment">// The update of the velocity is done according to the equation :</span>
<a name="l00610"></a>00610         <span class="comment">// v_{k+1} = N( (best_p+best_g)/2), sigma)</span>
<a name="l00611"></a>00611         <span class="comment">// with :</span>
<a name="l00612"></a>00612         <span class="comment">// sigma : the variance defined as |best_p_ij - best_g_ij| with</span>
<a name="l00613"></a>00613         <span class="comment">// best_p : the best position the particle ever had</span>
<a name="l00614"></a>00614         <span class="comment">// best_g : the best position the neighborhood ever had</span>
<a name="l00615"></a>00615 
<a name="l00616"></a>00616         <span class="keyword">typename</span> PARTICLE::VECTOR_TYPE&amp; pos_pbest = p.getBestPosition().getPosition();
<a name="l00617"></a>00617         <span class="keyword">typename</span> PARTICLE::VECTOR_TYPE&amp; pos_nbest = p.getNeighborhood().getBest()-&gt;getPosition();
<a name="l00618"></a>00618         <span class="keyword">typename</span> PARTICLE::VECTOR_TYPE&amp; vel = p.getVelocity();
<a name="l00619"></a>00619             
<a name="l00620"></a>00620         <span class="keywordtype">double</span> mean,var;
<a name="l00621"></a>00621         <span class="keywordflow">for</span>(<span class="keywordtype">size_t</span> i = 0 ; i &lt; pos_pbest.size() ; ++i)
<a name="l00622"></a>00622           {
<a name="l00623"></a>00623             mean = 0.5*(pos_pbest[i] + pos_nbest[i]);
<a name="l00624"></a>00624             var = fabs(pos_pbest[i] - pos_nbest[i]);
<a name="l00625"></a>00625             vel[i] = <a class="code" href="namespacepopot_1_1math.html#a379cb433fc4a216e75252d7278c161a7">popot::math::normal</a>(mean,var);
<a name="l00626"></a>00626           }
<a name="l00627"></a>00627       }
<a name="l00628"></a>00628 
<a name="l00629"></a>00629       
<a name="l00630"></a>00630       <span class="keyword">template</span>&lt;<span class="keyword">typename</span> PARTICLE&gt;
<a name="l00631"></a><a class="code" href="namespacepopot_1_1PSO_1_1particle.html#aef2d730ddf9001e22b5108576869c768">00631</a>       <span class="keywordtype">void</span> <a class="code" href="namespacepopot_1_1PSO_1_1particle.html#aef2d730ddf9001e22b5108576869c768">updateVelocity_modifiedBarebone</a>(PARTICLE&amp; p)
<a name="l00632"></a>00632       {
<a name="l00633"></a>00633         <span class="comment">// The update of the velocity is done according to the equation :</span>
<a name="l00634"></a>00634         <span class="comment">// v_{k+1} = N( (best_p+best_g)/2), sigma)</span>
<a name="l00635"></a>00635         <span class="comment">// with :</span>
<a name="l00636"></a>00636         <span class="comment">// sigma : the variance defined as |best_p_ij - best_g_ij| with</span>
<a name="l00637"></a>00637         <span class="comment">// best_p : the best position the particle ever had</span>
<a name="l00638"></a>00638         <span class="comment">// best_g : the best position the neighborhood ever had</span>
<a name="l00639"></a>00639         <span class="keywordtype">double</span> mean,var;
<a name="l00640"></a>00640                 
<a name="l00641"></a>00641         <span class="keyword">typename</span> PARTICLE::VECTOR_TYPE&amp; pos_pbest = p.getBestPosition().getPosition();
<a name="l00642"></a>00642         <span class="keyword">typename</span> PARTICLE::VECTOR_TYPE&amp; pos_nbest = p.getNeighborhood().getBest()-&gt;getPosition();
<a name="l00643"></a>00643         <span class="keyword">typename</span> PARTICLE::VECTOR_TYPE&amp; vel = p.getVelocity();
<a name="l00644"></a>00644 
<a name="l00645"></a>00645         <span class="keywordflow">for</span>(<span class="keywordtype">size_t</span> i = 0 ; i &lt; vel.size() ; ++i)
<a name="l00646"></a>00646           {
<a name="l00647"></a>00647             <span class="keywordflow">if</span>(<a class="code" href="namespacepopot_1_1math.html#aff66c3835eec2f4653b273e25e8c841b">popot::math::uniform_random</a>(0.0,1.0) &lt; 0.5)
<a name="l00648"></a>00648                 vel[i] = pos_pbest[i];
<a name="l00649"></a>00649             <span class="keywordflow">else</span>
<a name="l00650"></a>00650               {
<a name="l00651"></a>00651                 mean = 0.5*(pos_pbest[i] + pos_nbest[i]);
<a name="l00652"></a>00652                 var = fabs(pos_pbest[i] - pos_nbest[i]);
<a name="l00653"></a>00653                 vel[i] = <a class="code" href="namespacepopot_1_1math.html#a379cb433fc4a216e75252d7278c161a7">popot::math::normal</a>(mean,var);
<a name="l00654"></a>00654               }
<a name="l00655"></a>00655           }
<a name="l00656"></a>00656       }
<a name="l00657"></a>00657       
<a name="l00658"></a>00658 
<a name="l00659"></a>00659       
<a name="l00660"></a>00660   } <span class="comment">// namespace particle</span>
<a name="l00661"></a>00661 } <span class="comment">// namespace PSO</span>
<a name="l00662"></a>00662   
<a name="l00663"></a>00663   <span class="comment">// Artificial Bee Colony</span>
<a name="l00664"></a>00664 <span class="keyword">namespace </span>ABC
<a name="l00665"></a>00665 {
<a name="l00666"></a><a class="code" href="namespacepopot_1_1ABC_1_1individuals.html">00666</a>   <span class="keyword">namespace </span>individuals
<a name="l00667"></a>00667   {
<a name="l00668"></a>00668     <span class="keyword">class </span><a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html">FoodSource</a> : <span class="keyword">public</span> <a class="code" href="classpopot_1_1Vector.html" title="The template parameters is the type of values (e.g. double, or bool)">Vector</a>&lt;double&gt;
<a name="l00669"></a>00669     {
<a name="l00670"></a>00670       <span class="keyword">typedef</span> <a class="code" href="classpopot_1_1Vector.html">Vector&lt;double&gt;</a> TVector;
<a name="l00671"></a>00671 
<a name="l00672"></a>00672     <span class="keyword">private</span>:
<a name="l00673"></a>00673       <span class="keywordtype">double</span> _fitness;
<a name="l00674"></a>00674       <span class="keywordtype">double</span> _fvalue;
<a name="l00675"></a>00675       <span class="keywordtype">size_t</span> _counter;
<a name="l00676"></a>00676 
<a name="l00677"></a>00677     <span class="keyword">public</span>:
<a name="l00678"></a>00678 
<a name="l00679"></a><a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a54470b25db5ca1b8fb359a214e99a785">00679</a>       <a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a76b3aafb0ac19e27eb46a83193be0d5e">FoodSource</a>(<span class="keywordtype">size_t</span> dimension)
<a name="l00680"></a>00680         : <a class="code" href="classpopot_1_1Vector.html">TVector</a>(dimension),
<a name="l00681"></a>00681           _fitness(0), 
<a name="l00682"></a>00682           _fvalue(0), 
<a name="l00683"></a>00683           _counter(0)
<a name="l00684"></a>00684       {}
<a name="l00685"></a>00685 
<a name="l00686"></a><a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a76b3aafb0ac19e27eb46a83193be0d5e">00686</a>       <a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a76b3aafb0ac19e27eb46a83193be0d5e">FoodSource</a>(): <a class="code" href="classpopot_1_1Vector.html">TVector</a>(), _fitness(0), _fvalue(0), _counter(0)
<a name="l00687"></a>00687       {}
<a name="l00688"></a>00688 
<a name="l00692"></a><a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#ad4aeaed009cd9d5a62ae35fac28f9771">00692</a>       <a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a76b3aafb0ac19e27eb46a83193be0d5e">FoodSource</a>(<span class="keyword">const</span> <a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html">FoodSource</a> &amp; other) 
<a name="l00693"></a>00693         : <a class="code" href="classpopot_1_1Vector.html">TVector</a>(other), 
<a name="l00694"></a>00694           _fitness(other._fitness), 
<a name="l00695"></a>00695           _fvalue(other._fvalue), 
<a name="l00696"></a>00696           _counter(other._counter)
<a name="l00697"></a>00697       {
<a name="l00698"></a>00698       }
<a name="l00699"></a>00699 
<a name="l00700"></a><a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a7fdf76dfe2ab96ff9e085ee0d66efb0a">00700</a>       <span class="keyword">virtual</span> <a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a7fdf76dfe2ab96ff9e085ee0d66efb0a">~FoodSource</a>(<span class="keywordtype">void</span>)
<a name="l00701"></a>00701       {}
<a name="l00702"></a>00702 
<a name="l00703"></a>00703       <span class="keyword">template</span>&lt;<span class="keyword">typename</span> LBOUND_FUNC, <span class="keyword">typename</span> UBOUND_FUNC, <span class="keyword">typename</span> COST_FUNCTION&gt;
<a name="l00704"></a><a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#afa0b8dc32f3247cc303eca843b5965b4">00704</a>       <span class="keywordtype">void</span> <a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#afa0b8dc32f3247cc303eca843b5965b4">init</a>(<span class="keyword">const</span> LBOUND_FUNC&amp; lbound, <span class="keyword">const</span> UBOUND_FUNC &amp;ubound, <span class="keyword">const</span> COST_FUNCTION&amp; cost_function)
<a name="l00705"></a>00705       {
<a name="l00706"></a>00706         <span class="keywordflow">for</span>(<span class="keywordtype">size_t</span> i = 0 ; i &lt; this-&gt;<a class="code" href="classpopot_1_1Vector.html#a9630b07a8856a54b94d940083124a720">_dimension</a> ; ++i)
<a name="l00707"></a>00707           (*<span class="keyword">this</span>)[i] = <a class="code" href="namespacepopot_1_1initializer_1_1position.html#a76bfea94ffddf8b069755537a05f1bab">popot::math::uniform_random</a>(lbound(i), ubound(i));
<a name="l00708"></a>00708 
<a name="l00709"></a>00709         <a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a09c1767af12ba9b630647b6a5f82a41f">computeFitness</a>(cost_function);
<a name="l00710"></a>00710 
<a name="l00711"></a>00711         <span class="comment">// Initialize the counter</span>
<a name="l00712"></a>00712         _counter = 0;
<a name="l00713"></a>00713       }
<a name="l00714"></a>00714 
<a name="l00715"></a>00715       <span class="keyword">template</span>&lt;<span class="keyword">typename</span> LBOUND_FUNC, <span class="keyword">typename</span> UBOUND_FUNC, <span class="keyword">typename</span> COST_FUNCTION&gt;
<a name="l00716"></a><a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a6a3fe094b5d26a516d79ada8cd2ca840">00716</a>       <span class="keywordtype">void</span> <a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a6a3fe094b5d26a516d79ada8cd2ca840">combine</a>(<span class="keyword">const</span> <a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html">FoodSource</a> &amp; other_source, <span class="keyword">const</span> LBOUND_FUNC&amp; lbound, <span class="keyword">const</span> UBOUND_FUNC&amp; ubound, <span class="keyword">const</span> COST_FUNCTION&amp; cost_function)
<a name="l00717"></a>00717       {
<a name="l00718"></a>00718         <span class="comment">// Combination operator</span>
<a name="l00719"></a>00719         <span class="keywordtype">int</span> change_dim;
<a name="l00720"></a>00720         <span class="keywordtype">double</span> phi;
<a name="l00721"></a>00721         <a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html">FoodSource</a> new_source;
<a name="l00722"></a>00722         <span class="keywordtype">double</span> new_param_value;
<a name="l00723"></a>00723 
<a name="l00724"></a>00724         <span class="comment">// Randomly select a dimension to change</span>
<a name="l00725"></a>00725         change_dim = (int) <a class="code" href="namespacepopot_1_1math.html#aff66c3835eec2f4653b273e25e8c841b">popot::math::uniform_random</a>(0, this-&gt;<a class="code" href="classpopot_1_1Vector.html#a9630b07a8856a54b94d940083124a720">_dimension</a>);
<a name="l00726"></a>00726           
<a name="l00727"></a>00727         <span class="comment">// Random combination coefficient</span>
<a name="l00728"></a>00728         phi = <a class="code" href="namespacepopot_1_1initializer_1_1position.html#a76bfea94ffddf8b069755537a05f1bab">popot::math::uniform_random</a>(-1.0, 1.0);
<a name="l00729"></a>00729 
<a name="l00730"></a>00730         <span class="comment">// Compute the new source</span>
<a name="l00731"></a>00731         new_source = *<span class="keyword">this</span>;
<a name="l00732"></a>00732         new_param_value = (*this)[change_dim] + phi * ((*this)[change_dim] - other_source[change_dim]);
<a name="l00733"></a>00733 
<a name="l00734"></a>00734         <span class="comment">// Bound the parameter value</span>
<a name="l00735"></a>00735         <span class="keywordflow">if</span>(new_param_value &lt; lbound(change_dim))
<a name="l00736"></a>00736           new_param_value = lbound(change_dim);
<a name="l00737"></a>00737         <span class="keywordflow">else</span> <span class="keywordflow">if</span>(new_param_value &gt; ubound(change_dim))
<a name="l00738"></a>00738           new_param_value = ubound(change_dim);
<a name="l00739"></a>00739           
<a name="l00740"></a>00740         new_source[change_dim] = new_param_value;
<a name="l00741"></a>00741 
<a name="l00742"></a>00742         <span class="comment">// Evaluate the fitness of the new solution</span>
<a name="l00743"></a>00743         new_source.<a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a09c1767af12ba9b630647b6a5f82a41f">computeFitness</a>(cost_function);
<a name="l00744"></a>00744 
<a name="l00745"></a>00745         <span class="comment">// Perform a greedy selection</span>
<a name="l00746"></a>00746         <span class="keywordflow">if</span>(new_source.<a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a57d40e8ec95c358fd49d18dd7952dd07">getFitness</a>() &gt; <a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a57d40e8ec95c358fd49d18dd7952dd07">getFitness</a>())
<a name="l00747"></a>00747           {
<a name="l00748"></a>00748             <span class="comment">// If we get a better solution, it takes the place of the previous one</span>
<a name="l00749"></a>00749             *<span class="keyword">this</span> = new_source;
<a name="l00750"></a>00750             <a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#aa2f74d79de105b350f2f86fd58542a5b">resetCounter</a>();
<a name="l00751"></a>00751           }
<a name="l00752"></a>00752         <span class="keywordflow">else</span>
<a name="l00753"></a>00753           {
<a name="l00754"></a>00754             <span class="comment">// otherwise we increment its counter</span>
<a name="l00755"></a>00755             (*this)++;
<a name="l00756"></a>00756           }
<a name="l00757"></a>00757       }
<a name="l00758"></a>00758 
<a name="l00759"></a><a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#aa2f74d79de105b350f2f86fd58542a5b">00759</a>       <span class="keywordtype">void</span> <a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#aa2f74d79de105b350f2f86fd58542a5b">resetCounter</a>(<span class="keywordtype">void</span>)  {
<a name="l00760"></a>00760         _counter = 0;
<a name="l00761"></a>00761       }
<a name="l00762"></a>00762 
<a name="l00763"></a>00763 
<a name="l00764"></a><a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a6c716f57e22c22ce6479758f964e7f33">00764</a>       <span class="keywordtype">size_t</span> <a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a6c716f57e22c22ce6479758f964e7f33">getCounter</a>(<span class="keywordtype">void</span>) {
<a name="l00765"></a>00765         <span class="keywordflow">return</span> _counter;
<a name="l00766"></a>00766       }
<a name="l00767"></a>00767 
<a name="l00768"></a><a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a4886f6e69fb53d4a88cfa335a5aad38b">00768</a>       <span class="keywordtype">void</span> <a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a4886f6e69fb53d4a88cfa335a5aad38b">operator++</a>(<span class="keywordtype">int</span>) { 
<a name="l00769"></a>00769         _counter ++;
<a name="l00770"></a>00770       }
<a name="l00771"></a>00771 
<a name="l00772"></a>00772       <span class="keyword">template</span>&lt;<span class="keyword">typename</span> COST_FUNCTION&gt;
<a name="l00773"></a><a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a09c1767af12ba9b630647b6a5f82a41f">00773</a>       <span class="keywordtype">void</span> <a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a09c1767af12ba9b630647b6a5f82a41f">computeFitness</a>(<span class="keyword">const</span> COST_FUNCTION&amp; cost_function)  {
<a name="l00774"></a>00774         <span class="comment">// Compute the fitness</span>
<a name="l00775"></a>00775         _fvalue = cost_function(this-&gt;<a class="code" href="classpopot_1_1Vector.html#aa74fc0320c51efea093baeafb62a9c21">getValuesPtr</a>());
<a name="l00776"></a>00776         _fitness = <a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a297f0bcb60c8325b5c1a187577f2debd">fitnessFunction</a>(_fvalue);
<a name="l00777"></a>00777       }
<a name="l00778"></a>00778 
<a name="l00779"></a><a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a297f0bcb60c8325b5c1a187577f2debd">00779</a>       <span class="keywordtype">double</span> <a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a297f0bcb60c8325b5c1a187577f2debd">fitnessFunction</a>(<span class="keywordtype">double</span> x)  {
<a name="l00780"></a>00780         <span class="keywordflow">if</span>(x &gt;= 0.0)
<a name="l00781"></a>00781           <span class="keywordflow">return</span> 1.0 / (1.0 + x);
<a name="l00782"></a>00782         <span class="keywordflow">else</span>
<a name="l00783"></a>00783           <span class="keywordflow">return</span> 1.0 + fabs(x);
<a name="l00784"></a>00784       }
<a name="l00785"></a>00785 
<a name="l00786"></a><a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a57d40e8ec95c358fd49d18dd7952dd07">00786</a>       <span class="keywordtype">double</span> <a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a57d40e8ec95c358fd49d18dd7952dd07">getFitness</a>(<span class="keywordtype">void</span>)
<a name="l00787"></a>00787       {
<a name="l00788"></a>00788         <span class="keywordflow">return</span> _fitness;
<a name="l00789"></a>00789       }
<a name="l00790"></a>00790 
<a name="l00791"></a><a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a5ced1dda1140e9afb922d959499540d7">00791</a>       <span class="keywordtype">double</span> <a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a5ced1dda1140e9afb922d959499540d7">getFValue</a>(<span class="keywordtype">void</span>)
<a name="l00792"></a>00792       {
<a name="l00793"></a>00793         <span class="keywordflow">return</span> _fvalue;
<a name="l00794"></a>00794       }
<a name="l00795"></a>00795 
<a name="l00796"></a><a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#abf22131db51e548dddcfdf8b0a205c54">00796</a>       <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#abf22131db51e548dddcfdf8b0a205c54">print</a>(std::ostream &amp; os)<span class="keyword"> const</span>
<a name="l00797"></a>00797 <span class="keyword">      </span>{
<a name="l00798"></a>00798         <a class="code" href="classpopot_1_1Vector.html#a6d3cb96a58935fafa1020b7b5704effe">TVector::print</a>(os);
<a name="l00799"></a>00799         os &lt;&lt; <span class="stringliteral">&quot; Fitness : &quot;</span> &lt;&lt; _fitness &lt;&lt; <span class="stringliteral">&quot; Fvalue : &quot;</span> &lt;&lt; _fvalue &lt;&lt; <span class="stringliteral">&quot; Count : &quot;</span> &lt;&lt; _counter;
<a name="l00800"></a>00800       }
<a name="l00801"></a>00801 
<a name="l00805"></a><a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a48530294f7ef77dea9d3b331aa09599d">00805</a>       <span class="keyword">friend</span> std::ostream &amp; <a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#a48530294f7ef77dea9d3b331aa09599d">operator &lt;&lt;</a>(std::ostream &amp; os, <span class="keyword">const</span> <a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html">FoodSource</a> &amp;b)
<a name="l00806"></a>00806       {
<a name="l00807"></a>00807         b.<a class="code" href="classpopot_1_1ABC_1_1individuals_1_1FoodSource.html#abf22131db51e548dddcfdf8b0a205c54">print</a>(os);
<a name="l00808"></a>00808         <span class="keywordflow">return</span> os;
<a name="l00809"></a>00809       }
<a name="l00810"></a>00810 
<a name="l00811"></a>00811     }; <span class="comment">// FoodSource     </span>
<a name="l00812"></a>00812   } <span class="comment">// namespace individuals</span>
<a name="l00813"></a>00813 
<a name="l00814"></a>00814 } <span class="comment">// namespace ABC</span>
<a name="l00815"></a>00815 
<a name="l00816"></a>00816 }<span class="comment">//namespace popot</span>
<a name="l00817"></a>00817 
<a name="l00818"></a>00818 <span class="preprocessor">#endif // POPOT_INDIVIDUALS_H</span>
</pre></div></div>
</div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Friends</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><span class="SelectionMark">&#160;</span>Defines</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>



<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.7.5
</small></address>

</body>
</html>
